/**
 * 全局loading
 */
class Loading {
	constructor(options) {
		options = options || {};
		this._showCallback = options.showCallback || function () {};
		this._hideCallback = options.hideCallback || function () {};
		this._clearCallback = options.clearCallback || function () {};
	}
	// 次数
	count = 0;

	// 清除标识
	_cleared = false;

	/**
	 * 显示
	 * @returns
	 */
	show() {
		if (this.count === 0) {
			this._cleared = false;
			this._showCallback();
		}
		this.count++;
		return this;
	}

	/**
	 * 隐藏
	 * @returns
	 */
	hide() {
		if (this._cleared) return this;
		if (this.count > 0) this.count--;
		if (this.count === 0) {
			this._hideCallback();
		}
		return this;
	}

	/**
	 * 清除
	 */
	clear() {
		if (this._cleared) return;
		this.count = 0;
		this._cleared = true;
		this._clearCallback();
		return this;
	}
}

/**
 * 创建loading
 */
const createLoading = (function () {
	let instance = null;
	// 创建loading
	return function createLoadingFn(options) {
		if (!instance) {
			instance = new Loading(options);
		}
		return instance;
	};
})();

export default createLoading;
